package com.xixi.core.rpc.loadbalancer;

import com.xixi.core.rpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author: zxh
 * @date: 2024/8/27 14:25
 * @description: 随机负载均衡器
 */
public class RandomLoadBalancer implements LoadBalancer{

	private final Random random = new Random();

	@Override
	public ServiceMetaInfo select(Map<String, Object> requestParams, List<ServiceMetaInfo> serviceMetaInfoList) {
		int size = serviceMetaInfoList.size();
		if (size == 0) {
			return null;
		}
		// 只有1个服务，不用随机
		if (size == 1) {
			return serviceMetaInfoList.get(0);
		}
		return serviceMetaInfoList.get(random.nextInt(size));
	}
}
